package com.ruoyi.business.service.impl;

import com.ruoyi.business.mapper.WsdCompanyMapper;
import com.ruoyi.business.service.IWsdCompanyService;
import com.ruoyi.common.core.domain.dto.CompanyDTO;
import com.ruoyi.common.core.domain.entity.WsdCompany;
import com.ruoyi.common.core.domain.vo.CompanyVO;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 公司概览信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-24
 */
@Service
public class WsdCompanyServiceImpl implements IWsdCompanyService
{
    @Value("${upload.dir}")
    private String uploadDir;///home/data/mhstore/ruoyi/uploadPath

    @Value("${upload.url}")
    private String uploadUrl;//https://mhstore.mjunity.com/store
    @Autowired
    private WsdCompanyMapper wsdCompanyMapper;


    /**
     * 查询公司概览信息列表
     *
     * @param wsdCompany 公司概览信息
     * @return 公司概览信息
     */
    @Override
    public List<WsdCompany> selectWsdCompanyList(WsdCompany wsdCompany) {
        return wsdCompanyMapper.selectWsdCompanyList(wsdCompany);
    }

    @Override
    public CompanyVO selectWsdCompany() {
        // 从数据库查询出公司信息
        WsdCompany wsdCompanies = wsdCompanyMapper.selectWsdCompany();
        // 创建一个新的CompanyVO对象，用于视图层展示
        CompanyVO companyVO = new CompanyVO();
        // 创建两个列表，用于存储分割后的图片和证书信息
        List<String> imagesList = new ArrayList<>();
        List<String> certificateList = new ArrayList<>();
        List<String> bannerList = new ArrayList<>();
            // 使用"||"作为分隔符分割图片信息字符串
        String[] images = wsdCompanies.getImages().split("\\|\\|");
        for (String image : images) {
            imagesList.add(image);
        }
        // 使用"||"作为分隔符分割证书信息字符串
        String[] customizes = wsdCompanies.getCertificates().split("\\|\\|");
        for (String customize : customizes) {
            certificateList.add(customize);
        }
        if (wsdCompanies.getBanner()!=null){
            String[] banner = wsdCompanies.getBanner().split("\\|\\|");
            for (String banners : banner) {
                bannerList.add(banners);
            }
        }
        // 将从数据库查询的WsdCompany对象的属性复制到CompanyVO对象中
        BeanUtils.copyProperties(wsdCompanies, companyVO);
        // 设置CompanyVO对象的图片列表和证书列表
        companyVO.setImages(imagesList);
        companyVO.setCertificates(certificateList);
        companyVO.setBanner( bannerList );
        // 返回CompanyVO对象
        return companyVO;
    }


    /**
     * 新增公司概览信息
     *
     * @param  companyDTO 公司概览信息
     * @return 结果
     */
    @Override
    public int insertWsdCompany(CompanyDTO companyDTO) {
        String imagesAsString = String.join("||", companyDTO.getImages());
        String join = String.join("||", companyDTO.getCertificates());
        WsdCompany wsdCompany = new WsdCompany();
        BeanUtils.copyProperties(companyDTO,wsdCompany);
        wsdCompany.setImages(imagesAsString);
        wsdCompany.setCertificates(join);
        return wsdCompanyMapper.insertWsdCompany(wsdCompany);
    }

    /**
     * 修改公司概览信息
     *
     * @param companyDTO 公司概览信息
     * @return 结果
     */
    @Override
    public int updateWsdCompany(CompanyDTO companyDTO) {
        String imagesAsString = String.join("||", companyDTO.getImages());
        String join = String.join("||", companyDTO.getCertificates());
        String banner = String.join("||", companyDTO.getBanner());
        WsdCompany wsdCompany = new WsdCompany();
        BeanUtils.copyProperties(companyDTO,wsdCompany);
        wsdCompany.setCompanyId(companyDTO.getCompanyId());
        wsdCompany.setImages(imagesAsString);
        wsdCompany.setCertificates(join);
        wsdCompany.setBanner( banner );
        return wsdCompanyMapper.updateWsdCompany(wsdCompany);
    }

    /**
     * 批量删除公司概览信息
     *
     * @param companyIds 需要删除的公司概览信息主键
     * @return 结果
     */
    @Override
    public int deleteWsdCompanyByCompanyIds(Long[] companyIds)
    {
        return wsdCompanyMapper.deleteWsdCompanyByCompanyIds(companyIds);
    }


    @Override
    public String uploadFile(MultipartFile file) {
        if (file.isEmpty()) {
            return "请选择文件上传";
        }

        try {
            // 获取上传文件的原始文件名
            String originalFileName = file.getOriginalFilename();
            String fileExtension = "";
            if (originalFileName != null && originalFileName.lastIndexOf(".") != -1) {  //获取后缀
                fileExtension = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
            }
            UUID uuid = UUID.randomUUID();
            String imageName = uuid.toString().replace("-", "");

            String date= DateUtils.datePath();//11111111111111111

            String filePath =   date+ File.separator+imageName + "."+ fileExtension;

            // 构建上传目标路径，可以根据需要修改

            Path uploadPath = Paths.get(uploadDir); ///home/data/mhstore/ruoyi/uploadPath uploadDir
            Path filePath1 = uploadPath.resolve(filePath);
            File targetFile = filePath1.toFile();

            // 保存文件到本地文件系统
            //File targetFile = new File(filePath);
            if(!targetFile.getParentFile().exists()){
                targetFile.getParentFile().mkdirs();
            }
            String returnPath =  File.separator+"profile" +File.separator+ filePath;
            //String returnPath = File.separator+"mtUpload"+ File.separator+ filePath;
            file.transferTo(targetFile);

            return  uploadUrl + returnPath;
        } catch (IOException e) {
            //log.info(e.getMessage());
            return "文件上传失败：" + e.getMessage();
        }
    }

}
